﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FerrSimplicity
{
    /// <summary>
    /// This component will offset this object from another object's location.
    /// Very useful for camera stuff, or things of that nature.
    /// </summary>
    public class ComOffset : BaseComponent
    {
        #region Fields
        /// <summary>
        /// The object that this component is offset from
        /// </summary>
        protected BaseGameObject mFrom;
        #endregion

        #region Properties
        /// <summary>
        /// Motion is done using a Lerp, so a value of '1' would mean 1-1 movement with the target,
        /// and '0' would be no movement whatsoever. Defaults to 1
        /// </summary>
        public float   Smoothness { get; set; }
        /// <summary>
        /// How large of an offset is it? The offset object + this value determines the location this
        /// object smooths towards.
        /// </summary>
        public Vector3 Offset     { get; set; }
        #endregion

        #region Constructor
        /// <summary>
        /// A default parameterless constructor for use with cloning
        /// </summary>
        public ComOffset()
            : this(null, Vector3.Zero, 1) { }
        /// <summary>
        /// Creates an offset component
        /// </summary>
        /// <param name="aObj">The object our component should be following.</param>
        /// <param name="aOffset">How far away it should be following</param>
        public ComOffset(BaseGameObject aObj, Vector3 aOffset)
            : this(aObj, aOffset, 1) { }
        /// <summary>
        /// Creates an offset component
        /// </summary>
        /// <param name="aObj">The object our component should be following.</param>
        /// <param name="aOffset">How far away it should be following</param>
        /// <param name="aSmoothness">How smooth should the component follow the target? A value between 0 and 1, where 1 is 1-1 movement</param>
        public ComOffset(BaseGameObject aObj, Vector3 aOffset, float aSmoothness)
        {
            mFrom      = aObj;
            Offset     = aOffset;
            Smoothness = aSmoothness;
        }
        #endregion

        #region Methods
        public override void Update(GameTime aTime)
        {
            if (mFrom != null)
            {
                Parent.Transform.Position = Vector3.Lerp(Parent.Transform.Position, mFrom.Transform.Position + Offset, Smoothness);
            }
            base.Update(aTime);
        }
        #endregion

    }
}
